// Package service @program: seastargoMicroServiceDocker
//@author: edte
//@create: 2021-12-27 20:54
package service

import (
	useraccount "account/config"
	"account/database"
	log2 "account/log"
	"account/tools"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"strconv"
)

type UserPower struct {
	useraccount.UnimplementedUserServerServer
}

func (u UserPower) UserLogin(ctx context.Context, in *useraccount.LoginUserFormRequest) (response *useraccount.LoginUserFormResponse, err error) {
	in.UserPassword, err = tools.Md5(in.UserPassword)
	response = &useraccount.LoginUserFormResponse{}
	response.UserInfo = &useraccount.QueryUserFormResponse{}
	if err != nil {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLogin", in.UserPassword, "Error", "User password encryption error\t("+err.Error()+")")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		return response, err
	}
	user, err := database.UserLoginDb(ctx, in.UserPhone, in.UserPassword)
	if err != nil {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLogin", in.UserPhone, "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		return response, err
	}
	response.UserInfo.UserId = int32(user.Id)
	response.UserInfo.UserPhone = user.UserPhone
	response.UserInfo.UserPassword = user.Password
	response.UserInfo.Email = user.EmailNumber
	response.UserInfo.Nickname = user.NickName
	response.Status = http.StatusOK
	writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserLogin", strconv.Itoa(user.Id), "Debug", "user:"+strconv.Itoa(user.Id)+"login success")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	return response, err
}
func (u UserPower) UserRegister(_ context.Context, in *useraccount.RegisterUserFormRequest) (response *useraccount.RegisterUserFormResponse, err error) {
	//in.UserForm.UserPassword, err = tools.Md5(in.UserForm.UserPassword)
	response = &useraccount.RegisterUserFormResponse{}
	/*	if err != nil {
		response.Status = http.StatusBadRequest
		return response, err
	}*/
	user := tools.User{UserPhone: in.UserForm.UserPhone, Password: in.UserForm.UserPassword, EmailNumber: in.Email, NickName: in.Nickname}
	err = database.UserRegisterDb(user)
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserRegister", strconv.Itoa(user.Id), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		response.Status = http.StatusBadRequest
		return response, err
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "account", "UserRegister", strconv.Itoa(user.Id), "Debug", "user:"+strconv.Itoa(user.Id)+"\taccount registration is normal")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	response.Status = http.StatusOK
	return response, err
}
func (u UserPower) UserQueryData(ctx context.Context, in *useraccount.QueryUserFormRequest) (response *useraccount.QueryUserFormResponse, err error) {
	data, err := database.USerQueryData(ctx, int(in.UserId))
	response = &useraccount.QueryUserFormResponse{}
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "account", "UserQueryData", strconv.Itoa(int(in.UserId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return nil, err
	}
	var user tools.User
	err = json.Unmarshal([]byte(data), &user)
	if user.UserPhone == "" {
		writeLog, err := log2.WriteLog(ctx, "account", "UserQueryData", strconv.Itoa(int(in.UserId)), "Warring", "user:"+strconv.Itoa(int(in.UserId))+"Description Failed to query user information")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err != nil {
			fmt.Println("写入日志失败")
		}
		return nil, errors.New("description Failed to query user information")
	}
	response.UserPhone = user.UserPhone
	response.UserPassword = user.Password
	response.Email = user.EmailNumber
	response.UserId = int32(user.Id)
	response.Nickname = user.NickName
	writeLog, err := log2.WriteLog(context.Background(), "account", "UserQueryData", strconv.Itoa(int(in.UserId)), "Error", "The user"+strconv.Itoa(int(in.UserId))+" information is successfully queried")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err != nil {
		fmt.Println("写入日志失败")
	}
	return response, err
}
func (u UserPower) UserUpdate(_ context.Context, in *useraccount.UpdateUserFormRequest) (response *useraccount.UpdateUserFormResponse, err error) {
	response = &useraccount.UpdateUserFormResponse{}

	user := tools.User{UserPhone: in.UserForm.UserPhone,
		Password:    in.UserForm.UserPassword,
		EmailNumber: in.UserForm.Email, NickName: in.UserForm.Nickname, Id: int(in.UserForm.UserId)}
	err = database.UserUpdateDb(user)
	if err != nil {

		writeLog, err := log2.WriteLog(context.Background(), "account", "UserUpdate", strconv.Itoa(user.Id), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		return response, err
	}
	writeLog, err := log2.WriteLog(context.Background(), "account", "UserUpdate", in.UserPhone, "Debug", "The user information is updated successfully")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err != nil {
		fmt.Println("写入日志失败")
	}
	response.Status = http.StatusOK
	return response, err
}
